using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;

namespace M13.Game.Entity.Player
{
    public class PlayerStateMove : PlayerStateBase
    {
        private float _moveSpeed;


        private Vector3 _input;
        private Vector3 _skewedInput;
        private Vector3 _currentinput;
        private bool _isRotate;
        private List<float> _rotMathf = new List<float> { 0f, 45f, 90f, 135f, 180f };

        
        public PlayerStateMove(string stateName)
        {
            this.StateName = stateName;
        }
        
        public override void Enter()
        {
            _moveSpeed = 4f;
            _isRotate = false;
            _currentinput = player.transform.forward;

            animator.Play(GetAnimName(), 0, 0f);
        }
        public override void Update()
        {
            _input = new Vector3(inputCtrlSystem.AxisXInput, 0, inputCtrlSystem.AxisYInput);

            Matrix4x4 matrix = Matrix4x4.Rotate(Quaternion.Euler(0, cameraModal.CameraRot.Value.eulerAngles.y + 45f, 0));
            _skewedInput = matrix.MultiplyPoint3x4(_input).normalized;

            if (_input != Vector3.zero)
            {
                Quaternion target = Quaternion.LookRotation(_skewedInput);
                player.transform.rotation = Quaternion.Slerp(player.transform.rotation, target, 25 * Time.deltaTime);
                _currentinput = _skewedInput;
            }

            else stateMachine.SwitchState(typeof(PlayerStateIdle));

            

            // if (inputCtrlSystem.Execute0Input != 0f || inputCtrlSystem.Execute1Input != 0f)
            // {
            //     stateMachine.SwitchOnAction();
            //     return;
            // }

            if (inputCtrlSystem.AxisXInput != 0 && inputCtrlSystem.AxisYInput != 0) return;
            stateMachine.SwitchState(typeof(PlayerStateIdle));
        }
        public override void FixedUpdate()
        {
            player._collider.SimpleMove(_skewedInput * _moveSpeed);
        }

        public override bool TryEnter()
        {
            if (inputCtrlSystem.AxisXInput != 0 || inputCtrlSystem.AxisYInput != 0)
                return true;
            return false;
        }
        public override bool TryExit()
        {
            return true;
        }

        public override string GetAnimName()
        {
            return "1056_Run";
        }
        
        public override UniTask GetAnimPlayTask()
        {
            AnimPlayTask = UniTask.WaitUntil(() => GetAnimPlayProgress() > 0.99f && IsCurrentAnimPlaying(GetAnimName()));
            return AnimPlayTask;
        }

        /// <summary>
        /// 绘制输入方向
        /// </summary>
        public override void OnDrawGizmos()
        {
            Gizmos.color = Color.green;
            Gizmos.DrawLine(player.transform.position + new Vector3(0, 1.2f, 0),
                new Vector3(player.transform.position.x, player.transform.position.y + 1.2f, player.transform.position.z) + _skewedInput);
            Gizmos.color = Color.red;
            Gizmos.DrawLine(player.transform.position + new Vector3(0, 1.2f, 0), new Vector3(player.transform.position.x, player.transform.position.y + 1.2f, player.transform.position.z) + _input);
        }

    }
}